﻿//========================================================= 
/**@file VirtualDeviceCmdDriver.h
 * @brief 软件虚拟的设备
 * 
 * @date 2017-10-30   22:35:44
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_VIRTUALDEVICECMDDRIVER_H_
#define _LIBZHOUYB_VIRTUALDEVICECMDDRIVER_H_
//--------------------------------------------------------- 
#include "../CommonCmdDriver.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace driver {
//--------------------------------------------------------- 
/**
 * @brief 将接收的数据转为发送的数据设备驱动
 * @date 2017-10-30 22:20:05
 */
class LoopStreamCmdDriver : public CommonCmdDriver
{
protected:
    LoopStreamCmdAdapter _dev;
    Ref<IInteractiveTrans> _activeTrans;
public:
    LoopStreamCmdDriver() : CommonCmdDriver()
    {
        _activeTrans = _dev;

        _RegisteIInteractiveTransCMD(LoopStreamCmdDriver);
        _RegisteInterfaceCMD(LoopStreamCmdDriver, IInteractiveTrans);
    }
    LC_CMD_INTERFACE_GETTER(IInteractiveTrans, _activeTrans);
    LC_CMD_INTERACTIVETRANS(_dev);
};
//--------------------------------------------------------- 
/**
 * @brief 能抓取并缓存通信数据的设备驱动
 * @date 2017-11-06 20:35:10
 */
class CachedRecorderCmdDriver : public CommonCmdDriver
{
protected:
    CachedRecorder _dev;
    Ref<IInteractiveTrans> _activeTrans;
public:
    CachedRecorderCmdDriver() : CommonCmdDriver()
    {
        _activeTrans = _dev;

        _RegisteCMD(CachedRecorderCmdDriver, Save);
        _RegisteAdapterCMD(CachedRecorderCmdDriver);
        _RegisteIInteractiveTransCMD(CachedRecorderCmdDriver);
        _RegisteInterfaceCMD(CachedRecorderCmdDriver, IInteractiveTrans);
    }
    LC_CMD_INTERFACE_GETTER(IInteractiveTrans, _activeTrans);
    /**
     * @brief 保存记录的指令数据到文件中
     * @date 2017-11-06 20:39
     * 
     * @param [in] Path : string 需要保存的文件路径
     * @param [in] IsMerge : bool [default:true] 是否合并现有数据
     *
     * @retval Count : size_t 保存的指令数目
     * @retval CaptureCount : size_t 当前捕获到的指令条数
     */
    LC_CMD_METHOD(Save)
    {
        string path = arg["Path"].To<string>();
        bool isMerge = arg["IsMerge"].To<bool>(true);
        rlt.Put("CaptureCount", ArgConvert::ToString(_dev.Collection.Count()));
        if(isMerge)
        {
            FileVirtualDevice::Load(path.c_str(), _dev.Collection);
        }
        rlt.Put("Count", ArgConvert::ToString(_dev.Collection.Count()));
        return FileVirtualDevice::Save(path.c_str(), _dev.Collection);
    }
    LC_CMD_ADAPTER(IInteractiveTrans, _dev);
    LC_CMD_INTERACTIVETRANS(_activeTrans);
};
//--------------------------------------------------------- 
/**
 * @brief 以文件方式虚拟指令的设备
 * @date 2017-11-06 20:35:27
 */
class FileVirtualDeviceCmdDriver : public CommonCmdDriver
{
protected:
    FileVirtualDevice _dev;
    Ref<IBaseDevice> _activeDev;
    Ref<IInteractiveTrans> _activeTrans;
public:
    FileVirtualDeviceCmdDriver() : CommonCmdDriver()
    {
        _activeDev = _dev;
        _activeTrans = _dev;

        _logSetter.select(_dev);

        _RegisteIBaseDeviceCMD(FileVirtualDeviceCmdDriver);
        _RegisteIInteractiveTransCMD(FileVirtualDeviceCmdDriver);

        _RegisteInterfaceCMD(FileVirtualDeviceCmdDriver, IBaseDevice);
        _RegisteInterfaceCMD(FileVirtualDeviceCmdDriver, IInteractiveTrans);
    }
    LC_CMD_METHOD(Open)
    {
        string name = arg["Name"].To<string>();
        return _dev.Open(name.c_str());
    }
    LC_CMD_METHOD(IsOpen)
    {
        ASSERT_FuncErr(_dev.IsOpen(), DeviceError::DevAlreadyCloseErr);
        return true;
    }
    LC_CMD_METHOD(Close)
    {
        _dev.Close();
        return true;
    }
    LC_CMD_INTERFACE_GETTER(IBaseDevice, _activeDev);
    LC_CMD_INTERFACE_GETTER(IInteractiveTrans, _activeTrans);
    LC_CMD_INTERACTIVETRANS(_dev);
};
//--------------------------------------------------------- 
} // nemespace driver
} // namespace application
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_VIRTUALDEVICECMDDRIVER_H_
//========================================================= 